package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Order;
import com.example.health_life.dto.OrderWithItemsDTO;
import com.example.health_life.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public Response<List<Order>> getAllOrders() {
        List<Order> orders = orderService.getOrderList(null, null, null);
        return Response.newSuccess(orders);
    }

    @GetMapping("/{userId}/{status}")
    public Response<List<Order>> getOrdersListByUserIdAndStatus(@PathVariable Integer userId,
                                                            @PathVariable Byte status) {
        List<Order> orders = orderService.getOrderList(userId, status, null);
        return Response.newSuccess(orders);
    }

    @GetMapping("/by-status")
    public Response<List<Order>> getOrdersByStatus(@RequestParam Byte status) {
        List<Order> orders = orderService.getOrderList(null, status, null);
        return Response.newSuccess(orders);
    }

    @GetMapping("/by-user")
    public Response<List<Order>> getOrdersByUserId(@RequestParam Integer userId) {
        List<Order> orders = orderService.getOrderList(userId, null, null);
        return Response.newSuccess(orders);
    }

    @GetMapping("/by-user-with-items")
    public Response<List<OrderWithItemsDTO>> getOrdersByUserIdWithItems(@RequestParam Integer userId) {
        List<OrderWithItemsDTO> ordersWithItems = orderService.getOrderListWithItems(userId, null, null);
        return Response.newSuccess(ordersWithItems);
    }

    @GetMapping("/by-postal-code")
    public Response<List<Order>> getOrdersByPostalCode(@RequestParam String postalCode) {
        List<Order> orders = orderService.getOrderList(null, null, postalCode);
        return Response.newSuccess(orders);
    }

    @GetMapping("/{id:\\d+}")
    public Response<Order> getOrderById(@PathVariable Integer id) {
        Order order = orderService.getOrderById(id);
        return Response.newSuccess(order);
    }

    @PostMapping
    public Response<Integer> createOrder(@RequestBody Order order) {
        int id = orderService.createOrder(order);
        return Response.newSuccess(id);
    }

    @PutMapping("/{id}/status")
    public Response<Integer> updateOrderStatus(@PathVariable Integer id, 
                                            @RequestParam Byte status) {
        int result = orderService.updateOrderStatus(id, status);
        return Response.newSuccess(result);
    }

    @DeleteMapping("/{id}")
    public Response<Integer> deleteOrder(@PathVariable Integer id) {
        int result = orderService.deleteOrder(id);
        return Response.newSuccess(result);
    }

    @PutMapping("/{id}")
    public Response<Integer> updateOrder(@PathVariable Integer id,
                                       @RequestParam(required = false) String address,
                                       @RequestParam(required = false) Double totalPrice,
                                       @RequestParam(required = false) Byte status) {
        int result = orderService.updateOrder(id, address, totalPrice, status);
        return Response.newSuccess(result);
    }

    @DeleteMapping("/{id}/physical")
    public Response<Void> hardDeleteOrderController(@PathVariable Integer id) {
        try {
            orderService.hardDeleteOrder(id);
            return Response.newSuccess(null); 
        } catch (IllegalStateException e) {
            return Response.newFail(e.getMessage());
        } catch (Exception e) {
            return Response.newFail("删除订单时发生内部错误，请联系管理员。");
        }
    }

}
